<template>
  <!-- 全院分析 -->
  <div class="app-container">
    <el-row>
      <el-select v-model="query.time" placeholder="请选择" style="width: 80px">
        <el-option v-for="item in [
          {
            label: '出院',
            value: 'cysj_l'
          },
          {
            label: '入院',
            value: 'rysj_l'
          },
          {
            label: '结算',
            value: 'jsrq_l'
          }
        ]" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>

      <el-date-picker v-model="query.stime" value-format="yyyy-MM-dd" type="date" placeholder="开始日期"
        style="margin-left: 5px">
      </el-date-picker>
      <span style="margin-left: 2px">至</span>
      <el-date-picker v-model="query.etime" value-format="yyyy-MM-dd" type="date" placeholder="结束日期"
        style="margin-left: 5px">
      </el-date-picker>

      <el-select style="margin-left: 5px; width: 250px" v-model="query.ylfkfs" multiple collapse-tags filterable
        placeholder="医疗付款方式">
        <el-option v-for="item in ykfs" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>

      <el-button style="margin-left: 5px" type="primary" round @click="search">查询</el-button>
    </el-row>

    <el-row>
      <el-table :data="ls" style="width: 100%; margin-top: 5px" v-loading="loading" :span-method="objectSpanMethod"
        :row-style="rowStyle" border>
        <el-table-column prop="grp" label="" width="150" align="center">
        </el-table-column>
        <el-table-column prop="name" label="指标名称" align="center">
        </el-table-column>
        <el-table-column prop="value" label="指标值" :formatter="fmt0">
        </el-table-column>
        <el-table-column prop="占比" label="占比（%）" :formatter="fmt1" align="center">
        </el-table-column>
      </el-table>
    </el-row>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { UserModule } from '@/store/modules/user'
import { infoAllMetric } from '@/api/cloudwebapi'
import Pagination from '@/components/Pagination/index.vue'
import { cloneDeep } from 'lodash'
import { Form } from 'element-ui'
import { parseTime, dateFormat } from '@/utils'
import { dicList } from '@/api/datatranform'
@Component({
  name: 'HospAll',
  components: {
    Pagination
  }
})
export default class extends Vue {
  private query = {
    s: '',
    stime: '',
    etime: dateFormat(new Date(), 'yyyy-MM-dd'),
    time: 'jsrq_l',
    ylfkfs: ''
  };

  private rowStyle(r: any) {
    const { row } = r
    if (['count', 'grp', 'notGrp'].includes(row.field)) {
      return { 'font-weight': 'bold', color: 'blue' }
    }
    return {}
  }
  private loading: boolean = true;
  private spanArr: Array<number> = [];
  private position: number = 0;
  // value formatter
  private fmt0(row: any, c: any, val: any) {
    // 住院费保留两位小数
    if (
      row.field == 'zfy_sum' ||
      row.field == 'zfy_avg' ||
      row.field == 'sjzyts_l_avg' ||
      row.field == 'benchmarkFee' ||
      row.field == 'grpzfy' ||
      row.field == 'fyc'
    ) {
      return (val || 0).toFixed(2)
    }
    if (row.field == 'cmi' || row.field == 'weight_sum') {
      return (val || 0).toFixed(4)
    }
    return val
  }

  // 占比格式化
  private fmt1(row: any, c: any, val: any) {
    let cls = ['wk', 'nk', 'cz', 'gdbl_0', 'gdbl_1', 'gdbl_-1']
    // 总人数
    let totalCount = this.ls
      .filter((it: any) => it.field == 'count')
      .map((it: any) => it.value)[0]
    if (
      (row.field == 'notGrp' ||
        row.field == 'grp' ||
        row.field == 'dead_sum') &&
      totalCount > 0
    ) {
      return ((row.value / totalCount) * 100).toFixed(2)
    }
    // 总入组数
    let totalGrp = this.ls
      .filter((it: any) => it.field == 'grp')
      .map((it: any) => it.value)[0]
    if (cls.includes(row.field) && totalGrp > 0) {
      return ((row.value / totalGrp) * 100).toFixed(2)
    }

    // 000组、歧义组占比
    let totalNotGrp = this.ls
      .filter((it: any) => it.field == 'notGrp')
      .map((it: any) => it.value)[0]
    if ((row.field == 'zero' || row.field == 'qygrp') && totalNotGrp > 0) {
      return ((row.value / totalNotGrp) * 100).toFixed(2)
    }
  }

  onMergeLines() {
    this.ls.forEach((item, index) => {
      if (index === 0) {
        this.spanArr.push(1)
        this.position = 0
      } else {
        // 判断当前元素与上一个元素是否相同
        if (this.ls[index].grp === this.ls[index - 1].grp) {
          this.spanArr[this.position] += 1
          this.spanArr.push(0)
        } else {
          this.spanArr.push(1)
          this.position = index
        }
      }
    })
  }

  private objectSpanMethod(r: any) {
    const { row, column, rowIndex, columnIndex } = r
    if (columnIndex === 0) {
      const _row = this.spanArr[rowIndex]
      const _col = _row > 0 ? 1 : 0
      return {
        rowspan: _row,
        colspan: _col
      }
    }
  }

  private search() {
    this.loadAllMetric()
  }

  private dicLs: Array<any> = [];
  private ykfs: Array<any> = [];
  private async loadDicLs() {
    const { data } = await dicList({ size: 1000 })
    return data.content
  }

  private metric: any = {};

  private async loadAllMetric() {
    this.loading = true
    const q: any = cloneDeep(this.query)
    q.stime = q.stime.replaceAll('-', '')
    q.etime = q.etime.replaceAll('-', '')
    if (q.ylfkfs && q.ylfkfs.length > 0) {
      q.ylfkfs = q.ylfkfs.join(',')
    }
    const { data } = await infoAllMetric(q)
    // console.log(data.drgtc)

    this.metric = data || {}

    // 新增的DRG统筹、基金支付总额格式化
    this.metric.drgtc = this.metric.drgtc.toFixed(2)
    this.metric.jjzfze = this.metric.jjzfze.toFixed(2)

    // 费用差  修改：DRG统筹 - 基金支付总额
    // console.log('this.metric.drgtc', this.metric.drgtc); // undefined
    if (this.metric.drgtc && this.metric.jjzfze) {
      this.metric.fyc = this.metric.drgtc - this.metric.jjzfze
    } else {
      this.metric.fyc = ''
    }

    // this.metric.fyc = this.metric.benchmarkFee - this.metric.grpzfy;
    this.metric.cmi = this.metric.cmi / 100

    this.ls.forEach((r: any) => {
      if (r.fmt) {
        r.value = r.fmt(this.metric)
      } else {
        r.value = this.metric[r.field]
      }
    })
    this.loading = false
  }

  private ls: Array<any> = [
    {
      name: '出院人数',
      field: 'count',
      fmt: (r: any) => r.grp + r.notGrp,
      grp: '住院情况',
      value: 0
    },
    {
      name: '总住院费',
      field: 'zfy_sum',
      grp: '住院情况',
      value: 0
    },
    {
      name: '平均床日',
      field: 'sjzyts_l_avg',
      grp: '住院情况',
      value: 0
    },

    {
      name: '次均住院费',
      field: 'zfy_avg',
      grp: '住院情况',
      value: 0
    },
    {
      name: '死亡人数',
      field: 'dead_sum',
      grp: '住院情况',
      value: 0
    },
    {
      name: '入组病例数',
      field: 'grp',
      grp: '入组情况',
      value: 0
    },
    {
      name: 'DRG组数',
      field: 'drg',
      grp: '入组情况',
      value: 0
    },
    {
      name: 'MDC数量',
      field: 'mdc',
      grp: '入组情况',
      value: 0
    },
    {
      name: '总点数',
      field: 'weight_sum',
      grp: '入组情况',
      value: 0.0
    },
    {
      name: '总标准费用',
      field: 'benchmarkFee',
      grp: '入组情况',
      value: 0.0
    },

    {
      name: '入组总费用',
      field: 'grpzfy',
      grp: '入组情况',
      value: 0.0
    },
    // 新增DRG统筹、基金支付总额
    {
      name: 'DRG统筹',
      field: 'drgtc',
      value: 0,
      grp: '入组情况'
    },
    {
      name: '基金支付总额',
      field: 'jjzfze',
      value: 0,
      grp: '入组情况'
    },
    {
      name: '费用差',
      field: 'fyc',
      grp: '入组情况',
      value: 0.0
    },
    {
      name: 'CMI',
      field: 'cmi',
      value: 0.0,
      grp: '入组情况'
    },
    {
      name: '外科组',
      field: 'wk',
      value: 0,
      grp: '入组情况'
    },
    {
      name: '非手术室治疗组',
      field: 'cz',
      value: 0,
      grp: '入组情况'
    },
    {
      name: '内科组',
      field: 'nk',
      value: 0,
      grp: '入组情况'
    },
    {
      name: '高倍率',
      field: 'gdbl_1',
      value: 0,
      grp: '入组情况'
    },
    // {
    //   name: "正常倍率",
    //   field: "gdbl_0",
    //   value: 0,
    //   grp: "入组情况",
    // },
    {
      name: '低倍率',
      field: 'gdbl_-1',
      value: 0,
      grp: '入组情况'
    },
    {
      name: '未入组病例	',
      field: 'notGrp',
      grp: '未入组情况',
      value: 0
    },
    {
      name: '000组',
      field: 'zero',
      value: 0,
      grp: '未入组情况'
    },
    {
      name: '歧义组',
      field: 'qygrp',
      grp: '未入组情况',
      value: 0
    }
  ];

  async created() {
    let cDate = new Date()
    // cDate.setMonth(cDate.getMonth() - 3)
    cDate.setMonth(cDate.getMonth()) // 修改默认开始时间为当月1号
    cDate.setDate(1)
    this.query.stime = dateFormat(cDate, 'yyyy-MM-dd')
    this.dicLs = await this.loadDicLs()

    this.ykfs = this.dicLs
      .filter((it: any) => it.category == '医疗付款方式代码')
      .map((it: any) => ({ label: it.name, value: it.code }))
    this.onMergeLines()
    this.loadAllMetric()
  }
}
</script>

<style lang="scss">
.el-date-editor {
  width: 140px !important;
}

.error {
  color: red;
}

.pagination-container {
  float: right;
}

.el-dialog__body {
  padding: 10px !important;
}
</style>